﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace CRM
{
    public partial class GetMoreMes : System.Web.UI.Page
    {
        protected string strOldLog = "暂无记录..";


        protected override void OnPreLoad(EventArgs e)
        {
            Common.CheckMes.CheckState("User_ID", "Index.aspx", "用户登录信息丢失，请重新登录...");
            string PowerMes = Common.EncryptString.DeMyEnncrypt(Common.GetMes.GetCookies("Power_Mes"), 6);

            if (PowerMes.IndexOf("1_1") < 0)
            {
                Common.MessageBox.Show(Page, "你没有该功能的权限...", "Body.aspx", "BACK");
            }
            base.OnPreLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            string strServiceID = Common.GetMes.GetSession("User_ID");//从会话中得到用户Id
            //从查询字符串中得到客户Id
            string T_UserID = (string.IsNullOrEmpty
                (Common.GetMes.GetRequestQuery(Request, "UID"))) ? 
                "" : Common.GetMes.GetRequestQuery(Request, "UID");
            if (!string.IsNullOrEmpty(T_UserID))                //如果客户Id不为空
            {   //调用ServiceUser.GetServiceUserInfo方法查询数据库返回SqlDataReader
                SqlDataReader DataRead = BLL.ServiceUser.GetServiceUserInfo(T_UserID);
                if (DataRead.HasRows)  //如果包含了记录行
                {
                    DataRead.Read();   //读取记录行
                    //使用Common类中的相关方法来判断并返回SqlDataReader中的相关字段值
                    lblAdress.Text = BaseClass.Common.NullString(DataRead["T_Address"]);
                    lblCity.Text = BaseClass.Common.NullString(DataRead["T_Province"])+
                        "-"+BaseClass.Common.NullString(DataRead["T_City"]);
                    lblCom.Text = BaseClass.Common.NullString(DataRead["T_CompanyName"]);
                    lblDateTime.Text = DataRead["T_DateTime"].ToString();
                    lblMail.Text = BaseClass.Common.NullString(DataRead["T_Email"]);
                    lblFax.Text = BaseClass.Common.NullString(DataRead["T_Fax"]);
                    lblMoblie.Text = BaseClass.Common.NullString(DataRead["T_Mobile"]);
                    lblPerson.Text = BaseClass.Common.NullString(DataRead["T_Person"]);
                    LBLPost.Text = BaseClass.Common.NullString(DataRead["T_PostCode"]);
                    lblQQ.Text = BaseClass.Common.NullString(DataRead["T_QQ"]);
                    lblSex.Text= BaseClass.Common.NullString(DataRead["T_Sex"]);
                    lblTel.Text = BaseClass.Common.NullString(DataRead["T_Tel"]);
                    lblWeb.Text = BaseClass.Common.NullString(DataRead["T_WebUrl"]);
                    lblFrom.Text = BaseClass.Common.UserFrom(DataRead["T_UserFrom"]);
                    lblLeave.Text = BaseClass.Common.UserLeave(DataRead["T_UserLeave"]);
                    lblState.Text = BaseClass.Common.UserState(DataRead["T_UserState"], 
                                    DataRead["T_NowService"]);
                    lblClass.Text = BaseClass.Common.UserType(DataRead["T_UserType"]);
                    lblRemark.Text = BaseClass.Common.UserFrom(DataRead["T_ReMark"]);
                    HUserID.Value = DataRead["T_UserID"].ToString();
                }
                DataRead.Close();     //使用完后关闭SqlDataReader并同时关闭数据库连接
                DataRead.Dispose();   //释放SqlDataReader对象
            }
            if (!string.IsNullOrEmpty(strServiceID))  //提取特定用户对该客户的服务跟踪记录
            {   //调用GetServiceWorkInfo获取服务跟踪信息
                SqlDataReader DataRead = 
                    BLL.ServiceUser.GetServiceWorkInfo(T_UserID, strServiceID);
                if (DataRead.HasRows)   //如果包含记录行
                {
                    DataRead.Read();   //读取到第1行
                    //得到日志记录列表
                    strOldLog = 
                        DataRead["T_BusinessLogNote"].ToString().Replace("\n", "<br/>");
                    //得到工作状态
                    lblWorkState.Text = BaseClass.Common.WorkState(DataRead["T_SLogState"]);
                    lblWorkUpdate.Text = DataRead["T_DateTime"].ToString();
                    HT_SLogID.Value = DataRead["T_SLogID"].ToString();
                    //根据工作状态设置当前服务的状态
                    SetServiceState(DataRead["T_SLogState"].ToString());
                    BtnSaveWorkInfo.Enabled = true;
                }
                else
                {   //如果未找到数据，则显示无跟进记录
                    BtnSaveWorkInfo.Enabled = false;
                    strOldLog = "<font color=red>该客户暂无跟进记录..</font>";
                }
                DataRead.Close();     //关闭SqlDataReader
                DataRead.Dispose();   //释放SqlDatareader
            }
        }

        protected void BtnSaveWorkInfo_Click(object sender, EventArgs e)
        {   //如果用户输入了跟进记录
            if (!string.IsNullOrEmpty(txtNowWork.Value.Trim()))
            {   //调用UpdateServiceWorkLog更新数据库
                BLL.ServiceUser.UpdateServiceWorkLog(HT_SLogID.Value, 
                    GetServiceState(), "<br/>------" + 
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + 
                    "------<br/>" + txtNowWork.Value.Trim() +
                    "<br/>----------------------------------------");
                //执行客户端脚本iframeClose，关闭ymPropmt控件
                Common.MessageBox.myScriptMes(Page, "iframeClose();", "Suess");
            }
            if (RBFire.Checked)  //如果洽谈失败
            {   //更新用户的状态，取消对该客户的跟进状态
                BLL.ServiceUser.UpdateUserState(HUserID.Value);
            }
        }

        /// <summary>
        /// 获取工作状态信息
        /// </summary>
        /// <returns></returns>
        private string GetServiceState()
        {
            if (RBFire.Checked)
            {
                return "1";
            }
            if (RBSuess.Checked)
            {
                return "2";
            }
            return "0";
        }

        /// <summary>
        /// 设置工作状态
        /// </summary>
        /// <param name="strState"></param>
        private void SetServiceState(string strState)
        {
            switch (strState.ToString())
            {
                case"0":
                default:
                    RBing.Checked = true;
                    break;
                case "1":
                    RBFire.Checked = true;
                    BtnSaveWorkInfo.Enabled = false;
                    break;
                case "2":
                    RBSuess.Checked = true;
                    BtnSaveWorkInfo.Enabled = false;
                    break;
            }
        }
    }
}
